<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/3/20
 * Time: 上午11:58
 *
 * 功能：自动确认收货
 * 版本：v1.0
 *
 *
 * 规则：1分钟拉取一次待处理数据
 *
 * 注意：可以手动，如果手动处理，则可以修复指定一个分钟内的未正确确认收货的订单
 *      例如 php confirmReceipt.php 2018-03-20 11:19:00  修复指定时间的未确认收货的订单
 *
 *
 */


ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\Order as mOrder;
use Biz\service\trade\Order as sOrder;

/*
 * 按照以下规则获取订单列表
 *
 *
 * 订单状态：已发货
 * 订单autoconfirm_time为当前时间的分钟值
 *
 */

if (isset($argv[1])) {
    $stopMiunte = $argv[1];
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}


$startMiunte = '2018-04-01 00:00:00';



$where = [
    'status'      => mOrder::STATUS_SEND_OUT,
    'autoconfirm_time[<>]' => [$startMiunte,$stopMiunte],
];

try {
    //取出待处理的数据
    $orderList = mOrder::select("*",$where);

    if (empty($orderList)) {
        throw new \Exception("当前时刻没有待处理的订单数据");
    }

    foreach ($orderList as $order) {

        try {

            sOrder::confirmReceipt( 0,$order['order_no'], true);

        } catch (\Exception $e) {

            echo sprintf("%s    时刻:%s    order_no:%s   异常：%s  \n", date('Y-m-d H:i:s'),$stopMiunte, $order['order_no'],json_encode([
                'e_file'=>$e->getFile(),
                'e_line'=>$e->getLine(),
                'e_msg'=>$e->getMessage()
            ],JSON_UNESCAPED_UNICODE));


        }finally{
            echo sprintf("%s    时刻:%s   sql日志：%s   \n",date('Y-m-d H:i:s'),$stopMiunte,mOrder::sqlLogs());

            mOrder::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    时刻:%s   异常：%s  \n", date('Y-m-d H:i:s'),$stopMiunte,json_encode([
        'e_file'=>$e->getFile(),
        'e_line'=>$e->getLine(),
        'e_msg'=>$e->getMessage()
    ],JSON_UNESCAPED_UNICODE));

}







